package Login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 先判定用户的登录状态
        // 如果用户还没登录，要求先登录
        // 如果已经登录了，则根据 会话 上的用户名来显示到页面上
        HttpSession session = req.getSession(false);
        if (session == null) {
            // 此时未登录
            System.out.println("用户未登录！");
            // 返回到登录页面
            resp.sendRedirect("login.html");
            return;
        }
        // 此时为登录
        String username = (String)session.getAttribute("username");
        // 构造页面
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("欢迎" + username + "回来!");
    }
}
